//
//  ImageAndTextViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/24.
//  Copyright © 2020 王洋. All rights reserved.
////
//#import "WYXListCell.h"
//#import "TT_BaseTableView.h"


#import "ZZLayout.h"
#import "DetailTextCell.h"
#import "DetailImageViewCell.h"
#import "PackageDetailRemindCell.h"
#import "PackageDetailSerPromiseCell.h"
#import "DetailImageTextWebCell.h"

#import "DetailCaseCell.h"

#import "PackageDetailSectionHeader.h"
#import "ProductDetailNormalSectionView.h"

#import "ImageAndTextViewController.h"

@interface ImageAndTextViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, ZZLayoutDelegate,WKNavigationDelegate>



@property (nonatomic, copy) NSString *htmlString;

@property (nonatomic, assign) CGFloat contextCellHeight;//图文详情web高度

@property (nonatomic, assign) NSInteger lastcontentOffset; //添加此属性的作用，根据差值，判断ScrollView是上滑还是下拉

@property (nonatomic, assign) BOOL isLoadFinish;

@end

@implementation ImageAndTextViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self prepareData];
    [self initView];

}

- (void)prepareData {
    self.isLoadFinish = NO;
    self.contextCellHeight = 20.0f;
    
    //图文拼上平台权益的图
    
    self.htmlString = [Utilities fixHtmlStringWithUrlArray:self.model.images text:self.model.sellerGoodsModel.goodsDetailText];
}

- (void)initView {

    [self.view addSubview:self.collectionView];
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
    
}


#pragma mark- 协议方法
//collectionView的协议方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 3;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    switch (section) {
        case PackageDetailImageTextSectionType_web:{
            return 1;
        } break;
//        case PackageDetailImageTextSectionType_image:{
//            return self.model.imgTextList.count;
//        } break;
        case PackageDetailImageTextSectionType_serPromise:{
            return 0;
        } break;
        case PackageDetailImageTextSectionType_reminder:{
            return 1;
        } break;
        default:
            break;
    }
    return 0;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case PackageDetailImageTextSectionType_web:{

        } break;
//        case PackageDetailImageTextSectionType_image:{
//
//        } break;
//        case PackageDetailImageTextSectionType_serPromise:{
//
//            ProductDetailNormalSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView" forIndexPath:indexPath];
//            headerView.sectionTitle.text = @"婚前婚后服务承诺";
//            headerView.sectionSubTitle.text = @"";
//            return headerView;
//
//        } break;
        case PackageDetailImageTextSectionType_reminder:{

            ProductDetailNormalSectionView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView" forIndexPath:indexPath];
            headerView.sectionTitle.text = @"温馨提示";
            headerView.sectionSubTitle.text = @"";
            return headerView;
            
        } break;
        default:
            break;
    }
    
    UICollectionReusableView *reusableView = nil;
    // 区头
    if (kind == UICollectionElementKindSectionHeader) {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader" forIndexPath:indexPath];
        reusableView = headerView;
    }
    // 区尾
    if (kind == UICollectionElementKindSectionFooter) {
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
        reusableView = footerView;
    }
    return reusableView;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.section) {
        case PackageDetailImageTextSectionType_web:{

            DetailImageTextWebCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailImageTextWebCellID" forIndexPath:indexPath];
//            cell.contentLab.text = self.model.sellerGoodsModel.goodsDetailText;
            
            cell.navigationDelegate = self;

            if (!self.isLoadFinish && self.model.images.count > 0) {
                [cell reloadHtmlWithHtmlStr:self.htmlString];
            }            
            return cell;
            
        } break;
//        case PackageDetailImageTextSectionType_image:{
//
//            DetailImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailImageViewCellID" forIndexPath:indexPath];
//            [cell.mainImageView sd_setImageWithURL:[NSURL URLWithString:self.model.imgTextList[indexPath.row].url]];
//
//            return cell;
//
//        } break;
//        case PackageDetailImageTextSectionType_serPromise:{
//            PackageDetailSerPromiseCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailSerPromiseCellID" forIndexPath:indexPath];
//            return cell;
//
//        } break;
        case PackageDetailImageTextSectionType_reminder:{
            PackageDetailRemindCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PackageDetailRemindCellID" forIndexPath:indexPath];
            return cell;
        } break;
        default:
            break;
    }
    
    DetailImageViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailImageViewCellID" forIndexPath:indexPath];
    return cell;
}

//ZZLyout的流协议方法
- (CGFloat)layout:(ZZLayout *)layout heightForRowAtIndexPath:(NSIndexPath *)indexPath {//返回item的高

    switch (indexPath.section) {
//        case PackageDetailImageTextSectionType_text: {
//            return [Utilities getStringHeightWithText:self.model.sellerGoodsModel.goodsDetailText font:[UIFont systemFontOfSize:15] viewWidth:(TTScreenWidth - 30)] + 40;
//        } break;
        case PackageDetailImageTextSectionType_web: {
//            return TTScreenWidth;
            return _contextCellHeight + 20;

        } break;
        case PackageDetailImageTextSectionType_serPromise:{
            return CGFLOAT_MIN;
        } break;
        case PackageDetailImageTextSectionType_reminder: {
            return 130;
        } break;
        default:
            break;
    }
    return 200;
}

- (NSInteger)layout:(ZZLayout *)layout columnNumberAtSection:(NSInteger)section {//每个区有几列

    return 1;
}

- (UIEdgeInsets)layout:(ZZLayout *)layout insetForSectionAtIndex:(NSInteger)section {//设置每个区的边距
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

- (NSInteger)layout:(ZZLayout *)layout lineSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的行间距

    return 0;
}

- (CGFloat)layout:(ZZLayout*)layout interitemSpacingForSectionAtIndex:(NSInteger)section {//设置每个区的列间距

    return 0;
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForHeaderInSection:(NSInteger)section {//设置区头的高度

    switch (section) {
//        case PackageDetailImageTextSectionType_text:{
//        } break;
//        case PackageDetailImageTextSectionType_image:{
//        } break;
        case PackageDetailImageTextSectionType_web:{
        } break;
        case PackageDetailImageTextSectionType_serPromise:{
            return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
        } break;
        case PackageDetailImageTextSectionType_reminder:{
            return CGSizeMake(TTScreenWidth, 50);
        } break;
        default:
            break;
    }
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGSize)layout:(ZZLayout *)layout referenceSizeForFooterInSection:(NSInteger)section {//设置区尾的高度
    return CGSizeMake(TTScreenWidth, CGFLOAT_MIN);
}

- (CGFloat)layout:(ZZLayout*)layout spacingWithLastSectionForSectionAtIndex:(NSInteger)section {//本区区头和上个区区尾的间距
    return 0;
}

- (UIColor *)layout:(UICollectionView *)layout colorForSection:(NSInteger)section {//设置不同分区的不同背景颜色
    return [UIColor whiteColor];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (!self.canScroll) {
        scrollView.contentOffset = CGPointZero;
    }
    if (scrollView.contentOffset.y<=0) {
        self.canScroll = NO;
        scrollView.contentOffset = CGPointZero;
        [[NSNotificationCenter defaultCenter]postNotificationName:@"tabNoti" object:nil];
    }

    CGFloat hight = scrollView.frame.size.height;
    CGFloat contentOffset = scrollView.contentOffset.y;
    CGFloat distanceFromBottom = scrollView.contentSize.height - contentOffset;
    CGFloat offset = contentOffset - self.lastcontentOffset;
    self.lastcontentOffset = contentOffset;
    
    if (distanceFromBottom < hight) {
        if (offset > 0 && contentOffset > 0) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"packageShowQuanYiNoti" object:nil userInfo:nil];
        }
    }
}

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    NSLog(@"didFinishNavigation 调用一次");
    WEAKSELF
    [webView evaluateJavaScript:@"document.body.offsetHeight;" completionHandler:^(id _Nullable obj, NSError * _Nullable error) {
        
        weakSelf.contextCellHeight = [obj floatValue];
        [webView sizeThatFits:CGSizeMake(weakSelf.view.bounds.size.width, weakSelf.contextCellHeight)];

        //如果没加载完&&有详情信息
        if (!weakSelf.isLoadFinish) {

//        if (!weakSelf.isLoadFinish && self.model.goodsDetails.length) {
            [weakSelf.collectionView reloadData];
            weakSelf.isLoadFinish = YES;
        }

    }];
}

#pragma mark- 懒加载
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        
        ZZLayout *layout = [[ZZLayout alloc] initWith:ZZLayoutFlowTypeVertical delegate:self];
        //打开区头悬浮功能(默认关闭)
        layout.sectionHeadersPinToVisibleBounds = NO;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.backgroundColor = UIColorHex(0xf1f1f1);
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"DetailTextCell" bundle:nil] forCellWithReuseIdentifier:@"DetailTextCellID"];
        
        [_collectionView registerClass:[DetailImageTextWebCell class] forCellWithReuseIdentifier:@"DetailImageTextWebCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"DetailImageViewCell" bundle:nil] forCellWithReuseIdentifier:@"DetailImageViewCellID"];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailSerPromiseCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailSerPromiseCellID"];

        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailRemindCell" bundle:nil] forCellWithReuseIdentifier:@"PackageDetailRemindCellID"];

        [_collectionView registerNib:[UINib nibWithNibName:@"PackageDetailSectionHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"PackageDetailSectionHeader"];
        

        [_collectionView registerNib:[UINib nibWithNibName:@"ProductDetailNormalSectionView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ProductDetailNormalSectionView"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
        
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];

        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;

        _collectionView.bounces = YES;
        self.edgesForExtendedLayout = UIRectEdgeNone;
        if (@available(iOS 11.0, *)) {
            _collectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        
        WEAKSELF
        _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [weakSelf.collectionView.mj_footer endRefreshing];
        }];
        
    }
    return _collectionView;
}

@end
